package com.eleadmin.gps.utils;

/**
 * @author Damon
 * @create 2020-12-21 9:35
 */

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.utils.HttpsUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 高德地图导航工具类
 * @开发者 tiddler
 * @编写时间 2018年8月14日 上午10:01:05
 * @文件名 MapNavUtil.java
 * @类名 MapNavUtil
 */

public class MapNavUtil {
    private String startCoordinate;
    private String endCoordinate;
    private String applicationKey;
    private String param;
    /**
     * 必须要构造参数
     * @param startCoordinate 起点经纬度 经度在前，纬度在后
     * @param endCoordinate 终点经纬度 经度在前，纬度在后
     * @param applicationKey 高德地图应用key，需要Web服务类型的key
     */
    public MapNavUtil(String startCoordinate, String endCoordinate,
                      String applicationKey) {
        this.startCoordinate = startCoordinate;
        this.endCoordinate = endCoordinate;
        this.applicationKey = applicationKey;
        this.param="origin="+this.startCoordinate+"&destination="+this.endCoordinate+"&key="+this.applicationKey;
    }
    /**
     * 必须要构造参数
     * @param startCoordinate 经纬度 经度在前，纬度在后
     * @param endCoordinate 具体地址
     * @param applicationKey 高德地图应用key，需要Web服务类型的key
     * @param cc 没有用,构造方法需要 不然报错 所以写了一个cc
     */
    public MapNavUtil(String startCoordinate, String applicationKey, String endCoordinate, int cc) {
        this.startCoordinate = startCoordinate;
        this.endCoordinate = endCoordinate;
        this.applicationKey = applicationKey;
        this.param="&location="+this.startCoordinate+"&key="+this.applicationKey+"&radius=1000&extensions=all";
    }

    /**
     * 腾讯地图
     * 必须要构造参数
     * @param startCoordinate 经纬度 纬度在前，经度在后
     * @param applicationKey 腾讯地图应用key，需要Web服务类型的key
     */
    public MapNavUtil(String startCoordinate, String applicationKey) {
        this.startCoordinate = startCoordinate;
        this.applicationKey = applicationKey;
        this.param="&location="+this.startCoordinate+"&key="+this.applicationKey+"&get_poi=1";

    }

    /**
     * https://lbs.amap.com/api/webservice/guide/api/direction/
     * 获取地图导航返回值
     * @return
     */
    public MapNavResults getResults(){
//        https://restapi.amap.com/v3/direction/driving?origin=116.45925,39.910031&destination=116.587922,40.081577&output=xml&key=<用户的key>
        String sendGet = HttpsUtils.get2("https://restapi.amap.com/v3/direction/driving?"+param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
//        System.out.println("jsonObject:"+jsonObject.toString());
        String routeJsonString = jsonObject.get("route").toString();
//        System.out.println("routeJsonString:"+routeJsonString);
        JSONObject routeObject=JSONObject.fromObject(routeJsonString);
        JSONArray jsonArray = routeObject.getJSONArray("paths");
        JSONObject zuiJson = jsonArray.getJSONObject(0);
        MapNavResults mapResult=new MapNavResults();
        mapResult.setDistance(zuiJson.get("distance").toString());
        mapResult.setDuration(zuiJson.get("duration").toString());
        mapResult.setTolls(zuiJson.get("tolls").toString());
        return mapResult;
    }

    /**
     * https://lbs.amap.com/api/webservice/guide/api/georegeo/
     * 根据经纬度得到省市区
     * @parameter gps 116.310003,39.991957
     * @return
     */
    public MapNavResults getGdAddress(){
        String sendGet = HttpsUtils.get2("https://restapi.amap.com/v3/geocode/regeo?output=json" + param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        MapNavResults mapResult=new MapNavResults();
        if (jsonObject.getString("status").equals("1")) {
            JSONObject addressObj = jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent");
//            String province = addressObj.getString("province");
            String city = addressObj.getString("city");
            String district = addressObj.getString("district");
            String township = addressObj.getString("township");
            mapResult.setAddress(city + district + township);
        }
        return mapResult;
    }

    public MapNavResults getTxAddress(){
//    String sendGet = HttpsUtils.get2("https://apis.map.qq.com/ws/place/v1/search?boundary=nearby(27.88396,121.124054,1000,1)");
        String sendGet = HttpsUtils.get2("https://apis.map.qq.com/ws/geocoder/v1/?"+ param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
        MapNavResults mapResult=new MapNavResults();
        if (jsonObject != null) {
            if (jsonObject.has("status") && jsonObject.getInt("status") == 0) {
                JSONObject result = JSONObject.fromObject(jsonObject.get("result"));
                if (result != null) {
                    JSONObject addressComponent = JSONObject.fromObject(result.get("address_component"));
                    if (addressComponent != null) {
                        String city = addressComponent.getString("city");
                        String district = addressComponent.getString("district");
                        String township = addressComponent.getString("street");
                        mapResult.setAddress(city + district + township);
                    }
                }
            }
        }

        return mapResult;
    }



    public void getTxSuggestion(){
        String sendGet = HttpsUtils.get2("https://apis.map.qq.com/ws/place/v1/suggestion?region=宁波市&region_fix=0&key="+ UserConstant.TX_MAP_KEY+"&keyword=" + param);
        JSONObject jsonObject=JSONObject.fromObject(sendGet);
    }

    //https://lbs.amap.com/api/webservice/guide/api/direction/
    public static void main(String[] args) {
//        String origin="121.126018,27.889082";//出发点经纬度
//        String destination="120.81837,27.873662";//目的地经纬度
//        String key="d60bb37439be2b4a4ac186cf8bb085d2";//高德用户key
////        String key="dbf11535f05586d5f42255c9c3082c54";//高德用户key
//        MapNavUtil mapResult=new MapNavUtil(origin, destination, key);
//        System.out.println(mapResult.getResults().toString());

        //高德
//        MapNavUtil mapResult = new MapNavUtil("121.060986,29.259657", UserConstant.GAODE_MAP_KEY, "某某工厂", 0);
//        String res = mapResult.getGdAddress().getAddress();
//        System.out.println(res.replaceAll("�",""));

        //腾讯
        MapNavUtil mapResult = new MapNavUtil("29.861293,121.538372", UserConstant.TX_MAP_KEY);
        String res = mapResult.getTxAddress().getAddress();
        System.out.println(res);

        //用于获取输入关键字的补完与提示
//        String sendGet = HttpsUtils.get2("https://apis.map.qq.com/ws/place/v1/suggestion?key="+ UserConstant.TX_MAP_KEY+"&keyword=" + "和");
//        JSONObject jsonObject=JSONObject.fromObject(sendGet);

    }

}